using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Model;
using BLL;
using BLL.Impl;

namespace WebUI.Admin.Func
{
    public partial class Func_Modify : System.Web.UI.Page
    {

        private IAdminManager adminManager;
        private int funcSn = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            adminManager = BLLFactory.CreateAdminManager();
            if (null!=Request.QueryString["funcSn"]&&!String.IsNullOrEmpty(Request.QueryString["funcSn"].ToString().Trim()))
                funcSn = Int32.Parse(Request.QueryString["funcSn"].ToString().Trim());
            if (!IsPostBack)
            {
                if (0 < funcSn)
                    Display_Update(funcSn);
                else
                    Display_Add();
            }
        }

        private void Display_Add()
        {
            this.lbfuncSn.Visible = false;
            this.lblFuncSn.Visible = false;
            this.txtFatherSn.Text = "";
            this.txtFuncName.Text = "";
            this.txtFuncURL.Text = "";
        }

        private void Display_Update(int funcSn)
        {
            if (0 < funcSn)
            {
                Model.Func func = new Model.Func();
                func.FuncSN = funcSn;
                func = adminManager.GetFunc(func);
                this.lbfuncSn.Text = funcSn.ToString();
                this.txtFuncName.Text = func.FuncName;
                this.txtFuncURL.Text = func.FuncURL;
                this.txtFatherSn.Text = func.FatherFunc.FuncSN.ToString();
            }
        }

        protected void btnModify_Click(object sender, EventArgs e)
        {

            Model.Func func = new Model.Func();
            func.FuncName = this.txtFuncName.Text;
            func.FuncURL = this.txtFuncURL.Text;
            func.FatherFunc = new Model.Func();
            if(String.IsNullOrEmpty(this.txtFatherSn.Text.Trim()))
                func.FatherFunc.FuncSN=0;
            else
            func.FatherFunc.FuncSN = Int32.Parse(this.txtFatherSn.Text.Trim());
            if (!String.IsNullOrEmpty(this.lbfuncSn.Text.Trim()))
            {
                func.FuncSN = Int32.Parse(this.lbfuncSn.Text);
                adminManager.UpdateFunc(func);
            }
            else
            {
                adminManager.SaveFunc(func);
            }
            Response.Redirect("~/Admin/Func/Func_List.aspx");
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            if (0 < funcSn)
                Display_Update(funcSn);
            else
                Display_Add();
        }
    }
}
